#include "HttpProtocol.h"



HttpProtocol::HttpProtocol(QObject *parent) :
    BaseProtocol(parent)
{
    this->m_protocolType = ProtocolType::HttpProtocol;
}

QByteArray HttpProtocol::generateData(Package package)
{
    QByteArray data;
    data.append(package.body());

    return data;

}

QList<Package> HttpProtocol::parseData(QByteArray data)
{
    QList<Package> packageList;

    Package package;
         bool ok;
       QJson::Parser parser;
       qDebug()<<"HttpProtocol::parseData";

       QVariantMap result = parser.parse(data, &ok).toMap();
       if(ok){
           QString info = result["info"].toString();
                     qDebug()<<"info:"<<info;

                     if(info == "OK")
                     {
                         QVariant lives = result["lives"];

                         foreach (QVariant m,lives.toList()) {

                             QVariantMap details = m.toMap();
                             QString temperature = details["temperature"].toString();
                             QString humidity = details["humidity"].toString();
                             qDebug()<<"temp "<<details["temperature"].toString();
                             qDebug()<<"humidity "<<details["humidity"].toString();


                             package.setFunction(getGaoDeWeather);
                             QString tmp = temperature+","+humidity;
                             package.setBody(tmp.toLatin1());
                              packageList.append(package);
                         }
                     }

       }
   /*Package package;
    bool ok;
      QJson::Parser parser;
      qDebug()<<"BAProtocol::parseData";
      QByteArray _data;
      for(int i=0;i<data.size()-2;i++)
          _data[i]=data[i+1];
      qDebug()<<"_data:"<<_data;
      QVariantMap result = parser.parse(_data, &ok).toMap();
      if(ok){
          QVariantMap _result = result["result"].toMap();
          QString code = _result["code"].toString();
          qDebug()<<"code:"<<code;
          QString message = _result["msg"].toString();
          qDebug()<<"message:"<<message;
          QString title =  result["title"].toString();
          qDebug()<<"title:"<<title;
         if(message == QString::fromLocal8Bit("请输入正确token"))
         {
             qDebug()<<"invalid token";
             package.setFunction(InvalidToken);

             package.setBody("0");

              packageList.append(package);
         }
        if(title == QString::fromLocal8Bit("获取公钥信息"))
         //  if(title.toLocal8Bit() == QString::fromLocal8Bit("系统获取公钥信息"))
          //    if(title == "获取公钥信息")
          {
               if((code == "200")&&(message == "success"))
               {
              qDebug()<<"getpublickey sucess";
              package.setFunction(GetPublicKey);
              QVariantMap list = _result["list"].toMap();
              QVariantMap item = list["item"].toMap();
              QString publicKey = item["publicKey"].toString();
              package.setBody(publicKey.toLatin1());

               packageList.append(package);
               }
          }
           else if(title == QString::fromLocal8Bit("第三方系统登录获取token"))
           {
                qDebug()<<"gettoken...";
                if((code == "200")&&(message == "success"))
                {
                    qDebug()<<"gettoken sucess";
                    package.setFunction(GeToken);
                    QVariantMap list = _result["list"].toMap();
                    QVariantMap item = list["item"].toMap();
                    QString token = item["token"].toString();
                    package.setBody(token.toLatin1());

                    packageList.append(package);
                }
                else
                {
                    qDebug()<<"gettoken fail";
                }
            }
        else if(title == QString::fromLocal8Bit("获取BMS设备点位信息"))
        {
                qDebug()<<"getPointInfo...";
                if((code == "200")&&(message == "success"))
                {
                    qDebug()<<"getPointInfo sucess";
                    package.setFunction(GetPointInfo);
                    QVariantMap list = _result["list"].toMap();

                    QVariant item = list["item"];

                    foreach (QVariant m,item.toList()) {

                        QVariantMap details = m.toMap();
                        package.setMapBody(details);
                    }


                    packageList.append(package);
                }
                else
                {
                    qDebug()<<"getPointInfo fail";
                }
        }

          else

              qDebug()<<"getPUblickEY fail!!";

      }*/

    return packageList;
}
